package net.sk.china.city.controller;

import java.util.List;

import net.sk.china.city.domain.vo.AddArticle;
import net.sk.china.city.domain.vo.UpdateArticle;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import net.sk.china.city.controller.base.BaseController;
import net.sk.china.city.domain.dto.AjaxResult;
import net.sk.china.city.domain.ArArticle;
import net.sk.china.city.service.IArArticleService;
import net.sk.china.city.domain.dto.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文章管理Controller
 * 
 * @author gxf
 * @date 2024-03-27
 */
@Api(tags = "资讯")
@RequiredArgsConstructor
@RestController
@RequestMapping("/information/article")
public class ArArticleController extends BaseController
{
    @Autowired
    private IArArticleService arArticleService;

    /**
     * 查询文章管理列表
     */
    @ApiOperation("文章列表")
    @PreAuthorize("hasAnyAuthority('information:article:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArArticle arArticle)
    {
        startPage();
        List<ArArticle> list = arArticleService.selectArArticleList(arArticle);
        return getDataTable(list);
    }

    /**
     * 导出文章管理列表
     */
//    @PreAuthorize("@ss.hasPermi('information:article:export')")
//    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, ArArticle arArticle)
//    {
//        List<ArArticle> list = arArticleService.selectArArticleList(arArticle);
//        ExcelUtil<ArArticle> util = new ExcelUtil<ArArticle>(ArArticle.class);
//        util.exportExcel(response, list, "文章管理数据");
//    }

    /**
     * 获取文章管理详细信息
     */
    @ApiOperation("文章详情")
    @PreAuthorize("hasAnyAuthority('information:article:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(arArticleService.selectArArticleById(id));
    }

    /**
     * 新增文章管理
     */
    @ApiOperation("新增文章")
    @PreAuthorize("hasAnyAuthority('information:article:add')")
    @PostMapping
    public AjaxResult add(@RequestBody AddArticle addArticle)
    {
        return toAjax(arArticleService.insertArArticle(addArticle));
    }

    /**
     * 修改文章管理
     */
    @ApiOperation("修改文章")
    @PreAuthorize("hasAnyAuthority('information:article:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody UpdateArticle updateArticle)
    {
        return toAjax(arArticleService.updateArArticle(updateArticle));
    }

    /**
     * 删除文章管理
     */
    @ApiOperation("删除文章")
    @PreAuthorize("hasAnyAuthority('information:article:remove')")
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(arArticleService.deleteArArticleById(id));
    }


//    @ApiOperation("上传文章图片")
//    @PostMapping("/uploadArticleImg")
//    public AjaxResult uploadArticleImg(@RequestPart("articleImg") MultipartFile file) throws Exception
//    {
//        if (!file.isEmpty())
//        {
//            String avatar = FileUploadUtils.upload(RuoYiConfig.getArticlePath(), file, MimeTypeUtils.IMAGE_EXTENSION);
//           return success(avatar);
//        }
//        return error("上传图片异常，请联系管理员");
//    }

    /**
     * 修改文章状态
     */
    @ApiOperation("修改文章状态")
    @PreAuthorize("hasAnyAuthority('information:article:edit')")
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody ArArticle arArticle)
    {
        return success(arArticleService.updateStatus(arArticle));
    }
}
